<?php 
	Class document_attache_m extends CI_Model
	{
		public function insertFile()
	    {	
	    	$id_file = "GMDA".rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9);
	    	if($this->db->select('*')->from('GMDocumentAttache')->where('GMDocumentAttache.GMDA_CODE', $id_file)->get())
	    	{
	    		$id_file = "GMDA".rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9);
	    	}
			$data = array();
			if($_FILES['GMDA_DOCUMENT']['error'] == 0)
			{
    			$relative_url = 'files/document_attache/'.$this->upload->file_name;
    			$data['GMDA_DOCUMENT'] = $relative_url;
    			$data['GMDA_NOM'] =  $_POST['GMDA_NOM'];
    			$data['GMDA_LANGUE'] =  $_POST['GMDA_LANGUE'];
    			$data['GMDA_STATUT'] = "actif" ;
    			if($_POST['GMET_CODE'])
    			{
    				$data['GMET_CODE'] = $_POST['GMET_CODE'];
    			}
    			else if($_POST['GMIN_CODE'])
    			{
    				$data['GMIN_CODE'] = $_POST['GMIN_CODE'];
    			}
    			$data['GMDA_CODE'] =  $id_file;
			}
			$this->db->insert('GMDocumentAttache', $data);
		}

		public function getAllFilesById($id)
		{
			$file = $this->db->select('*')->from('GMDocumentAttache')->where('GMDocumentAttache.GMET_CODE', $id)->get();
			return $file->result();
		}

		public function desactivateFile()
		{	
			$where = array(
							'GMDA_CODE' => $_POST['GMDA_CODE'],
							'GMDA_STATUT' => 'actif'
						);
			$data = array(
    						'GMDA_STATUT' => 'desactive'
						);
			$this->db->where($where)->update('GMDocumentAttache', $data);
		}

		public function activateFile()
		{	
			$where = array(
							'GMDA_CODE' => $_POST['GMDA_CODE'],
							'GMDA_STATUT' => 'desactive'
						);
			$data = array(
    						'GMDA_STATUT' => 'actif'
						);
			$this->db->where($where)->update('GMDocumentAttache', $data);
		}

		public function desactivateAllFilesById()
		{	
			$where = array(
							'GMET_CODE' => $_POST['GMET_CODE'],
							'GMDA_STATUT' => 'actif'
						);
			$data = array(
    						'GMDA_STATUT' => 'desactive'
						);
			$this->db->where($where)->update('GMDocumentAttache', $data);
		}

		public function activateAllFilesById()
		{	
			$where = array(
							'GMET_CODE' => $_POST['GMET_CODE'],
							'GMDA_STATUT' => 'desactive'
						);
			$data = array(
    						'GMDA_STATUT' => 'actif'
						);
			$this->db->where($where)->update('GMDocumentAttache', $data);
		}

		public function deleteFile()
	    {	
			$where = array(
							'GMDA_CODE' => $_POST['GMDA_CODE']	
    					);
			$this->db->delete('GMDocumentAttache', $where);
		}
	}
?>